var mainObj = document.getElementById("main");
var VirusArray = [];
var yiMiao=[];
var first=-1;
var zhi=-1;
var iSpeed;
var buttp=document.getElementById("btp");
var ope=document.getElementById("open");
var re=document.getElementById("restart");
var run_btn = true;
var vr;
var yi;
var vmo;
var ymo;
//height: 800px;width:1170px;

function Virus(imgSrc, x, y, speed) {
    this.imgNode = document.createElement("img");
    this.imgNode.classList.add("rotate-scale-down");
    this.imgNode.classList.add("image");
    this.imgSrc = imgSrc;
    this.x = x;
    this.y = y;
    this.speed = speed;

    this.init = function () {
        this.imgNode.src = this.imgSrc;
        this.imgNode.style.position = "absolute";
        this.imgNode.style.left = this.x + "px";
        this.imgNode.style.top = this.y + "px";

        mainObj.appendChild(this.imgNode);
        // element.appendChild()向元素添加新的子节点，作为最后一个子节点。
    }
    this.init();

    this.move = function () {
        //移动，可能用不到
        this.imgNode.style.top = parseInt(this.imgNode.style.top) + this.speed + "px";
        this.imgNode.style.left = parseInt(this.imgNode.style.left) + this.speed + "px";
    }
}
//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){ 
    switch(arguments.length){ 
        case 1: 
            return parseInt(Math.random()*minNum+1,10); 
        break; 
        case 2: 
            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
        break; 
            default: 
                return 0; 
            break; 
    } 
} 
function CreateVirus() {

    var n = "images/bingdu" + randomNum(1,3) + ".png";
    i=0;
    var virus1 = new Virus(n, parseInt(Math.random() * 620), parseInt(Math.random() * 350), parseInt(Math.random() * 4) + 1);
    /*var n="bingdu"+i;
    virus1.classList.add(n);*/
    //速度为0-10 之间随机数 +1 保证不为0
    VirusArray.push(virus1);
}

function CreateYiMiao() {
    i=0;
    var yiMiao1 = new Virus("images/yiMiao.png", parseInt(Math.random() * 620), parseInt(Math.random() * 350), parseInt(Math.random() * 10) + 1);
    /*var n="bingdu"+i;
    virus1.classList.add(n);*/
    //速度为0-10 之间随机数 +1 保证不为0
    yiMiao.push(yiMiao1);
}

function clear(){

    clearInterval (vr);
    clearInterval (yi);
    clearInterval (vmo);
    clearInterval (yno);

    for (var i = 0; i < yiMiao.length; i++){
        mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
        yiMiao.splice(i, 1);
    }
    for (var i = 0; i < yiMiao.length; i++){
        mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
        yiMiao.splice(i, 1);
    }

    for (var i = 0; i < VirusArray.length; i++){
        mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
        VirusArray.splice(i, 1);
    }
    for (var i = 0; i < VirusArray.length; i++){
        mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
        VirusArray.splice(i, 1);
    }

    re.style.display="block";
}

function yiMiaoMove() {
    
    for (var i = 0; i < yiMiao.length; i++) {
        yiMiao[i].move();
        if ((parseInt(yiMiao[i].imgNode.style.top) >= 750) || (parseInt(yiMiao[i].imgNode.style.left) >= 1120)) {
            
            mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
            yiMiao.splice(i, 1);//数组中移除
        }
    
    }


    if(iSpeed<=0){
        clear();
        for (var i = 0; i < yiMiao.length; i++){
            mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
            yiMiao.splice(i, 1);
        }
    
        for (var i = 0; i < VirusArray.length; i++){
            mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
            VirusArray.splice(i, 1);
        }
        alert("挑战失败")
    }
}

function VirusMove() {
    
    for (var i = 0; i < VirusArray.length; i++) {
        VirusArray[i].move();
        if ((parseInt(VirusArray[i].imgNode.style.top) >= 750) || (parseInt(VirusArray[i].imgNode.style.left) >= 1120)) {
            mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
            VirusArray.splice(i, 1);//数组中移除
            iSpeed=iSpeed-1;//进度条
            jindu.style.width=iSpeed+"%";
        }
        if(parseInt(iSpeed)<=0){
            clear();
            alert("很遗憾 保卫失败！");
        }
    }
}
function op(){
    if(first==-1){
        alert("请先选择难度");
    }
 
    if(run_btn&&first>-1){
        buttp.style.display="none";
        ope.style.display="none";
        re.style.display="none";

        vr=setInterval(CreateVirus, 1000);
        // 每隔1000ms执行一次，后期要修改此数据
        yi=setInterval(CreateYiMiao, 3000);

        vmo=setInterval(VirusMove, 50);
        
        yno=setInterval(yiMiaoMove, 50);

    }

}

function dianJiYiMiao(event,obj)
{
    x=event.clientX- obj.offsetLeft;
    y=event.clientY - obj.offsetTop;
    // console.log(y + " ?= " +event.clientY + " - " + obj.offsetTop);

    for(var i = 0;i < yiMiao.length;i ++){
        var w = yiMiao[i].imgNode.style.left;
        var h = yiMiao[i].imgNode.style.top;

        if(((x>=parseInt(w))&&(x<=(parseInt(w)+50)))&&((y>=parseInt(h))&&(y<=(parseInt(h)+50)))){  //数据记得换啊！！，100 * 80 是玩家图像大小，但是此处用的是光标位置在病毒图像内部才算是true，后期可以调整一下
            mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
            yiMiao.splice(i, 1);//数组中移除
            if(iSpeed<= 100){
                
                iSpeed=iSpeed+1;
                if(iSpeed <=100){
                    jindu.style.width=iSpeed+"%";
                }
                else{
                    jindu.style.width=100+"%";
                }
                
                
            }
        }

    }
    if(iSpeed>=100){
        
        jindu.style.width=100+"%";
        clear();
        for (var i = 0; i < yiMiao.length; i++){
            mainObj.removeChild(yiMiao[i].imgNode);//移除越界子节点
            yiMiao.splice(i, 1);
        }
    
        for (var i = 0; i < VirusArray.length; i++){
            mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
            VirusArray.splice(i, 1);
        }
        if(first != -1){
            alert("挑战成功");
        }
        first = -1;
    }
    
}

function btonClick1(){
    first=1;
    iSpeed=90;
    jindu.style.width=iSpeed+"%";
}
function btonClick2(){
    first=2;
    iSpeed=50;
    jindu.style.width=iSpeed+"%";
}
function btonClick3(){
    first=3;
    iSpeed=20;
    jindu.style.width=iSpeed+"%";
}

function dianJi(event,obj)
{
    x=event.clientX - obj.offsetLeft;
    y=event.clientY - obj.offsetTop;
    
    for(var i = 0;i < VirusArray.length;i ++){
        var w = VirusArray[i].imgNode.style.left;
        var h = VirusArray[i].imgNode.style.top;

        if(((x>=parseInt(w))&&(x<=(parseInt(w)+50)))&&((y>=parseInt(h))&&(y<=(parseInt(h)+50)))){  //数据记得换啊！！，100 * 80 是玩家图像大小，但是此处用的是光标位置在病毒图像内部才算是true，后期可以调整一下
            
            mainObj.removeChild(VirusArray[i].imgNode);//移除越界子节点
            VirusArray.splice(i, 1);//数组中移除

        }
    }
}

function res(){
    first = -1;
    iSpeed = 0;
    jindu.style.width=iSpeed+"%";
    window.location.reload();
}



